<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" preIcon="ant-design:import-outlined" @click="uploadAssign">上传作业</a-button>
      </template>
      <!-- 状态列 -->
      <!-- <template #status="{ record }">
        <Tag :color="record.status == 0 ? 'green' : 'red'">
        
        </Tag>
      </template> -->
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <SchoolAssignmentRecordsModal @register="registerModal" @success="handleSuccess"></SchoolAssignmentRecordsModal>
   <ReviewModel ref="reviewModelRef" :types='submitTypes'/>
  </div>
</template>

<script lang="ts" name="student_student-schoolAssignmentRecords" setup>
  import { ref, reactive, computed, unref } from 'vue';
  import ReviewModel from './components/reviewModel.vue'
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';
  import SchoolAssignmentRecordsModal from './components/assignSubmitModal.vue';
  import { columns, searchFormSchema, superQuerySchema } from './assignSubmit.data';
  import { list, deleteOne, batchDelete, getImportUrl, getExportUrl } from './assignSubmit.api';
  import { downloadFile } from '/@/utils/common/renderUtils';
  import { useUserStore } from '/@/store/modules/user';
  import { Tag } from 'ant-design-vue';
  const [registerModal, { openModal }] = useModal();
 const reviewModelRef =ref(null)
 const submitTypes = ref(1)
 
  const queryParam = reactive<any>({});
  const checkedKeys = ref<Array<string | number>>([]);
  const userStore = useUserStore();

  //注册table数据
  const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: 'assignment_records',
      api: list,
      columns,
      canResize: false,
      formConfig: {
        //labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: true,
        fieldMapToNumber: [],
        fieldMapToTime: [],
      },
      actionColumn: {
        width: 120,
        fixed: 'right',
      },
      beforeFetch: (params) => {
        return Object.assign(params, queryParam);
      },
    },
    exportConfig: {
      name: 'school_assignment_records',
      url: getExportUrl,
      params: queryParam,
    },
    importConfig: {
      url: getImportUrl,
      success: handleSuccess,
    },
  });

  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

  // 高级查询配置
  const superQueryConfig = reactive(superQuerySchema);

  /**
   * 高级查询事件
   */
  function handleSuperQuery(params) {
    Object.keys(params).map((k) => {
      queryParam[k] = params[k];
    });
    reload();
  }

  /**
   * 编辑事件
   */
  function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
      showFooter: true,
    });
  }
  /**
   * 详情
   */
  function handleDetail(record) {
    console.log('detail',record)
    console.log(reviewModelRef.value)
   reviewModelRef.value.openModel()
 
  }
  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, handleSuccess);
  }
  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
  }
  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }
  const uploadAssign = () => {
    // 调用上传文件的函数
    // uploadFile(url, data)
    openModal(true, {
      isUpdate: false,
      showFooter: true,
    });
  };
  /**
   * 操作栏
   */
  function getTableAction(record) {
    if (record.status === 0) {
      return [
        {
          label: '删除',
          color: 'error',
         
          onClick: handleDelete.bind(null, record),
        },
      ];
    } else {
      return [
        {
          label: '查看结果',
          onClick: handleDetail.bind(null, record),
        },
      ];
    }
  }
  // 根据状态生成不同的操作按钮
  const getTableActions = (record) => {
    if (record.status === 'active') {
      return [
        {
          label: '禁用',
          onClick: handleDisable.bind(null, record),
        },
      ];
    } else {
      return [
        {
          label: '激活',
          onClick: handleEnable.bind(null, record),
        },
      ];
    }
  };
  /**
   * 下拉操作栏
   */
  function getDropDownAction(record) {
    return [
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
      },
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
          placement: 'topLeft',
        },
        auth: 'student_student:school_assignment_records:delete',
      },
    ];
  }
  const handleEnable = (record) => {
    console.log('激活操作', record);
    // 调用API激活操作
  };

  const handleDisable = (record) => {
    console.log('禁用操作', record);
    // 调用API禁用操作
  };
</script>

<style lang="less" scoped>
  :deep(.ant-picker),
  :deep(.ant-input-number) {
    width: 100%;
  }
</style>
